package com.xm2.demo.system.employee.dao;

import com.xm2.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Create
 * @create 2018-12-17 18:58
 * @Description：
 */
@Repository
public interface EmployeeRepository extends JpaRepository<User, Integer> {

    @Query(value = "select count(id) from User u where u.positionId = :pid")
    int findAllByPid(@Param("pid") Integer pid);

    @Query(nativeQuery = true, value = "select * from user as u where u.position_id = :pid limit :startRow,:size")
    List<User> findUserByPidPage(@Param("pid") Integer pid, @Param("startRow") Integer startRow, @Param("size") Integer size);

    @Query(value = "select s.storesName from Stores as s")
    List<String> findStoreByPId();

    @Query(value = "select c.centerName from Center as c where c.id > 0")
    List<String> findCenterByPId();

    @Modifying
    @Transactional
    @Query(value = "delete from User u where u.id in (:ids) ")
    void deleteByIds(@Param("ids") List<Integer> ids);

    @Query(value = "from User where positionId = :pid and phone = :phone")
    User findUserByPhone(@Param("phone") String phone, @Param("pid") Integer pid);

    @Query(value = "from User where positionId > 0 and phone = :phone")
    User findUserByPhone(@Param("phone") String phone);
}
